package org.jeecg.modules.scheduling.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.classes.entity.Classes;
import org.jeecg.modules.clockRecord.entity.ClockRecord;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Description: 排班表
 * @Author: jeecg-boot
 * @Date:   2025-01-21
 * @Version: V1.0
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class CardVO implements Serializable {
    // 在类顶部定义颜色常量
    private static final String ABNORMAL_COLOR = "#FC9D9A";//  异常颜色
    private static final String DEPUTY_DAY_COLOR = "#44B3E1";// 副课长及以上上班
    private static final String DEPUTY_NIGHT_COLOR = "#AEDD81";// 副课长及以上下班
    private static final String STAFF_DAY_COLOR = "#CCFFFF";// 员工上班
    private static final String STAFF_NIGHT_COLOR = "#C1F0C8";// 员工下班

    private String name;//姓名
    private String inTime;//上班时间
    private String outTime;//下班时间
    private String faceUrl;//人像图片
    private String workNumber;//工号
    private String admitGuid;//人员guid
    private Integer abnormalType;//异常类型
    private java.lang.Integer abnormal;//是否有异常
    private java.lang.Integer solve;//是否已处理
    private Integer deputySupervisor;//是否副课长及以上
    private String color;//颜色

    // 新增方法：将ClockRecord对象转换为CardVO对象
    public static CardVO fromClockRecord(ClockRecord clockRecord, Classes classes) {
        CardVO cardVO = new CardVO();
        cardVO.setName(clockRecord.getAdmitName());
        cardVO.setInTime(clockRecord.getInTime());
        cardVO.setOutTime(clockRecord.getOutTime());
        cardVO.setFaceUrl(clockRecord.getFaceUrl());
        cardVO.setWorkNumber(clockRecord.getWorkNumber());
        cardVO.setAdmitGuid(clockRecord.getAdmitGuid());
        cardVO.setAbnormalType(clockRecord.getAbnormalType());
        cardVO.setAbnormal(clockRecord.getAbnormal());
        cardVO.setSolve(clockRecord.getSolve());
        cardVO.setDeputySupervisor(clockRecord.getDeputySupervisor());
        // 优化后的颜色判断逻辑
        String color;
        if (isAbnormalCase(clockRecord)) {
            color = ABNORMAL_COLOR;
        } else {
            boolean isDeputy = Integer.valueOf(1).equals(clockRecord.getDeputySupervisor());// 副课长及以上
            boolean hasOutTime = !StringUtils.isEmpty(clockRecord.getOutTime());// 是否有下班时间
            String currentTime = DateUtils.formatDate("HH:mm:ss"); // 单次获取当前时间

            if (hasOutTime) {// 如果有下班时间
                boolean isBeforeEnd = currentTime.compareTo(classes.getEndTime()) < 0;// 是否在下班时间之前
                color = isDeputy ?
                        (isBeforeEnd ? DEPUTY_DAY_COLOR : DEPUTY_NIGHT_COLOR) :
                        (isBeforeEnd ? STAFF_DAY_COLOR : STAFF_NIGHT_COLOR);
            } else {//  如果没有下班时间
                color = isDeputy ? DEPUTY_DAY_COLOR : STAFF_DAY_COLOR;
            }
        }
        cardVO.setColor(color);
        return cardVO;
    }

    // 新增辅助方法
    private static boolean isAbnormalCase(ClockRecord record) {
        return record.getAbnormal() == 1 && record.getSolve() == 0;
    }

}